package grails.company

import grails.converters.JSON

class ExtHelperService {

    def OrganizationAsTreeNode(Organization o, boolean deep = false) {
        def map =
            [
                    'id': o.id,
                    'text': o.title,
                    'expandable': true,
                    'leaf': true,
                    'nodeType': 'async',
                    'domain': Organization.domain,
                    'subdomain': Organization.subdomain
            ]

        if (deep) {
            map += [
                    'children': o.departments.collect { DepartmentAsTreeNode(it, deep) }
            ]
        }

        (map as JSON).toString()
    }

    def DepartmentAsTreeNode(Department d, boolean deep = false) {
        def map =
            [
                    'id': d.id,
                    'text': d.title,
                    'expandable': true,
                    'leaf': true,
                    'nodeType': 'async',
                    'domain': 'department',
                    'subdomain': 'project'
            ]

        if (deep) {
            map += [
                    'children': d.projects.collect { ProjectAsTreeNode(it, deep) }
            ]
        }

        (map as JSON).toString()
    }

    def ProjectAsTreeNode(Project p, boolean deep = false) {
        def map =
            [
                    'id': p.id,
                    'text': p.title,
                    'expandable': true,
                    'leaf': true,
                    'nodeType': 'async',
                    'domain': 'project',
                    'subdomain': 'employee'
            ]

        if (deep) {
            map += [
                    'children': p.employees.collect { EmployeeAsTreeNode(it, deep) }
            ]
        }

        (map as JSON).toString()
    }
}